package my.peerencrypt;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

public class Client {
	
	public static final String keyStore = "/key.properties";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Client client = new Client();
		System.out.println(client.genRegistRandom());
		String userName = "XM001";
		
		String ec = client.getEc(userName);
		//client.saveEc(userName, "3341-0305-2043-0010");
		if(ec!=null && !"".equals(ec)){
			System.out.println("已注册");
			client.login(userName, "123");
		}else{
			System.out.println("还未注册");
			//client.regist(userName, "123");
			ec = client.getEc(userName);
			client.login(userName, "123");
		}
		
		
		
	}

	public String genRegistRandom(){
		Random r = new Random(System.currentTimeMillis());
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<16;i++){
			sb.append(r.nextInt(8+1));
			if((i+1)%4==0 && i<15){
				sb.append("-");
			}
		}
		return sb.toString();
	}
	
	public int regist(String userName,String password,String registrandom){
		
		String key;
		int[] src = Encrypt.strToIntArr(registrandom);
		int[] a = new int[11];
		String ec = Encrypt.encrypt(src, a);
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<11;i++){
			sb.append(a[i]);
			if(i<10)sb.append("|");
		}
		key = sb.toString();
		Socket socket = null;
		try {
			socket = new Socket("127.0.0.1",7878);
			DataInputStream din = new DataInputStream(socket.getInputStream());
			DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
			
			dout.writeInt(2);
			sendString(dout, userName);
			sendString(dout, password);
			sendString(dout, registrandom);
			sendString(dout, key);
			dout.flush();
			int i=0;
			for(i=0;i<10;i++){
				if(din.available()<4){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			if(i==0){
				System.out.println("服务器超时");
				return -1;
			}
			int r = din.readInt();
			if(r==2){
				System.out.println("注册成功。。。");
				saveEc(userName, ec);
				return 2;
			}else{
				System.out.println("注册失败。。。。");
				return 3;
			}
			
			
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return -1;
	}
	
	public int login(String userName,String password){

		String ec = this.getEc(userName);
		if(ec==null || "".equals(ec)){
			return 1;
		}
		Socket socket = null;
		try {
			socket = new Socket("127.0.0.1",7878);
			DataInputStream din = new DataInputStream(socket.getInputStream());
			DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
			
			dout.writeInt(1);
			sendString(dout, userName);
			sendString(dout, password);
			sendString(dout, ec);
			
			int r = din.readInt();
			
			socket.shutdownInput();
			socket.shutdownOutput();
			socket.close();
			if(r==0){
				System.out.println("登录成功。。。");
				return 0;
			}else{
				System.out.println("登录失败。。。。");
				return 1;
			}
			
			
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return -1;
	
	}
	
	public void saveEc(String userName,String ec){
		Properties p = new Properties();
		try {
			p.load(Client.class.getResourceAsStream(keyStore));
			p.setProperty(userName, ec);
			File file = new File(Client.class.getResource(keyStore).getFile());
			System.out.println(file.getAbsolutePath());
			FileOutputStream fout = new FileOutputStream(file);
			
			p.store(fout, new Date().toString());
			fout.flush();
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getEc(String userName){
		Properties p = new Properties();
		try {
			p.load(Client.class.getResourceAsStream(keyStore));
			return p.getProperty(userName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	private void sendString(DataOutputStream dout,String s){
		byte[] b = s.getBytes();
		int l = b.length;
		try {
			dout.writeInt(l);
			dout.write(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
